import _ from 'lodash';
const baseSize = 144;
export interface ExportInfoType {
  level: number;
  parentId: number;
  itemTotal: number;
  AuxiliaryTotal: number;
  isDelete: boolean;
  AuxiliaryId: number;
}
const getDomFontsize = () => {
  let rootNode = document.documentElement;

  // 获取计算后的样式
  let computedStyle = window.getComputedStyle(rootNode);

  // 读取字体大小（注意这里返回的可能是一个包含单位的字符串，如"16px"）
  let fontSize = computedStyle.fontSize;

  // 如果你需要一个数字值（不带单位）进行计算，你可能需要解析这个字符串
  // 例如，使用正则表达式或split()方法去除单位，并将结果转换为数字
  let fontSizeNumber = parseFloat(fontSize.replace(/[^0-9.-]/g, ''));
  return fontSizeNumber;
};
export const getRemNumber = (e: number) => {
  let remNumber = getDomFontsize();
  // const scale = parseFloat((baseSize * (document.documentElement.clientHeight / 900)).toFixed(4));
  // 获取当前rem大小
  return (e / remNumber) * Math.min(remNumber, 2) + 'rem';
  //   document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px';
};
export const getRemHeightNumber = (e: number) => {
  let remNumber = getDomFontsize();
  // const scale = parseFloat((baseSize * (document.documentElement.clientHeight / 900)).toFixed(4));
  // 获取当前rem大小
  return (e / remNumber) * Math.min(remNumber, 2) + 'rem';
  //   document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px';
};
export const getRemConversion = (e: number) => {
  let remNumber = getDomFontsize();
  // const scale = parseFloat((baseSize * (document.documentElement.clientHeight / 900)).toFixed(4));

  return e / remNumber;
  //   document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px';
};
export const getDraggableNumber = (e: number) => {
  //将当前rem转化成具体数字
  const scale = getDomFontsize();
  let arr = e * scale;
  return arr ? arr : 0;
  //   document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px';
};
export const getRemReduction = (e: number) => {
  const scale = getDomFontsize();
  // 设置页面根节点字体大小
  return e * scale;
  //   document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px';
};
window.onresize = function () { };
export const getExhibitsHeight = (e: number) => {
  var viewportHeight = getDomFontsize();
  let arr = e / viewportHeight;

  return arr;
};
export const getExhibitsHeightRest = (e: number) => {
  let viewportHeight = getDomFontsize();
  // console.log(e * viewportHeight)
  return e ? e * viewportHeight : 0;
};
export const getdisabledY = (e: number) => {
  var viewportHeight = window.innerHeight * 0.9;
  return viewportHeight < e;
};
export const addExhibitsItemPosition = (arr: any) => {
  const maxScore = arr.reduce((max, obj) => Math.max(max, obj.x + obj.width), -Infinity);

  // let x=0
  // arr.forEach(element => {
  //   x+=element.width?element.width:0
  // });
  // console.log(maxScore ,x,'输出当前最大值'); // 输出: 98
  return maxScore;
  // arr
  //添加新的展品分组定位添加坐标

  // var viewportHeight = window.innerHeight*0.7;
  // return viewportHeight<e;
};
export const getFallbackType = (targetX: number, x: number) => {
  return targetX > x;
};
const truncateToFourDecimalPlaces = (num) => {
  var parts = num.toString().split('.');
  parts[1] = parts[1] ? parts[1].slice(0, 4) : '0000';
  return parseFloat(parts.join('.'));
};
export const collisionDetection = (arr: any, w: number, x: number, indexs: number) => {
  // console.log('触发')
  //获取是否与前后元素发生碰撞
  let activeNumber = w + x; //获取右侧极限
  let finalNumber = 0;
  let numbers = 0;
  let after = 0;
  arr.forEach((element, index) => {
    if (
      (x > element.x && x < element.x + element.width) ||
      (element.x > x && element.x + element.width < activeNumber)
    ) {
      numbers = element.x + (element.width - x);
      numbers = truncateToFourDecimalPlaces(numbers); //碰撞位置

      return numbers;
    }
    if (activeNumber > element.x && activeNumber < element.x + element.width) {
      after = activeNumber - element.x;
      after = -truncateToFourDecimalPlaces(after); //碰撞位置

      return after;
    }
    if (activeNumber === element.x + element.width && index !== indexs) {
      after = -w;
      // return numbers
    }
  });

  if (numbers !== 0 && after !== 0) {
    finalNumber = numbers;
  } else if (numbers !== 0) {
    finalNumber = numbers;
  } else if (after !== 0) {
    finalNumber = after;
  }
  // console.log('未发生碰撞',finalNumber)
  return finalNumber; //判断则为没有发生碰撞
};
const getsuperpositionNumber = (child: any, collisionNUmber: number, indexs: number) => {
  let clonedObject = _.cloneDeep(child);
  // console.log(clonedObject)
  let clearanceNumber = collisionNUmber > 0 ? collisionNUmber : -collisionNUmber;
  clonedObject.showpieceList.forEach((e) => {
    //进行判断如果大于拖拽的x轴则进行平移否则不产生平移
    //e.x > arr.showpieceList[indexs].x //(activeNumber > e.x && activeNumber < e.x + e.width) || activeNumber < e.x
    if (e.x > clonedObject.showpieceList[indexs].x) {
      if (e.x + e.width < clonedObject.showpieceList[indexs].x + clonedObject.showpieceList[indexs].width) {
        //判断当前拖动元素是否与后方元素叠加如果叠加则减去他们的叠加区域+平移距离
        clearanceNumber =
          clonedObject.showpieceList[indexs].x +
          clonedObject.showpieceList[indexs].width -
          e.x +
          (collisionNUmber > 0 ? collisionNUmber : -collisionNUmber);
      } else {
      }
    }
  });
  return clearanceNumber;
};
export const setcollisionArr = (child: any, collisionNUmber: number, indexs: number) => {
  //设置当前展品列表碰撞后移动状态
  let clonedObject = _.cloneDeep(child);
  let clearanceNumber = getsuperpositionNumber(child, collisionNUmber, indexs);
  // console.log(clonedObject)
  clonedObject.showpieceList.forEach((e, index) => {
    //进行判断如果大于拖拽的x轴则进行平移否则不产生平移

    if (
      e.x > clonedObject.showpieceList[indexs].x ||
      (clonedObject.showpieceList[indexs].x + clonedObject.showpieceList[indexs].width === e.x + e.width &&
        index !== indexs)
    ) {
      //   //判断当前拖动元素是否与后方元素叠加如果叠加则减去他们的叠加区域+平移距离

      e.x = e.x + clearanceNumber;
    }
    if (e.x < 0) {
      e.x = 0;
    }
  });
  if (collisionNUmber > 0) {
    clonedObject.showpieceList[indexs].x = clonedObject.showpieceList[indexs].x + collisionNUmber;
  } else {
    clonedObject.width = clonedObject.width + (collisionNUmber > 0 ? collisionNUmber : -collisionNUmber);
  }

  return clonedObject;
};

export const groupPostionCalculate = (arr: any) => {
  //判断分组大小移动之后内部元素是否需要调整
  let clonedObject = _.cloneDeep(arr);
  if (clonedObject.width < clonedObject.ShowcaseWidth + clonedObject.ShowcaseX) {
    clonedObject.ShowcaseWidth = (clonedObject.width - clonedObject.ShowcaseX) * 0.9;
  }

  clonedObject.showpieceList.forEach((element) => {
    if (element.x + element.width > clonedObject.width) {
      element.x = (clonedObject.width - element.width) * 0.9;
    }
  });
  return clonedObject;
};

export const getMOdeListArr = (list: any, id: number) => {
  //获取当前id归属数组
  let arrayList = [];
  list.forEach((element, elementIndex) => {
    if (element.id == id) {
      arrayList = [elementIndex];
    }
    element.children.forEach((el, elIndex) => {
      if (el.id == id) {
        arrayList = [elementIndex, elIndex];
      }
      el.children.forEach((arr, arrIndex) => {
        if (arr.id == id) {
          arrayList = [elementIndex, elIndex, arrIndex];
        }
      });
    });
  });
  return arrayList;
  //获取当前选择大纲下标排列
};
export const getMOdeListcorrelation = (list: any, id: number, AuxiliaryIndex: number) => {
  //获取当前id所有的关联信息

  let info: ExportInfoType = {
    level: 0, //当前所属等级
    parentId: 0, //父级id
    itemTotal: 0, //同级元素总数
    AuxiliaryTotal: 0, //辅助信息条数
    isDelete: false, //是否可以删除
    AuxiliaryId: 0, //辅助信息id
  };
  list.forEach((element, elementIndex) => {
    if (element.id == id) {
      info.level = 1;
      info.parentId = 0;
      info.itemTotal = list.length;
      info.AuxiliaryTotal = element.auxiliaryList.length;
      if (element.children.length === 0 && element.auxiliaryList.length === 0) {
        info.isDelete = true;
      }
      if (AuxiliaryIndex !== -1) {
        info.isDelete = true;
        info.AuxiliaryId = element.auxiliaryList[AuxiliaryIndex].id;
      }
    }
    element.children.forEach((el, elIndex) => {
      if (el.id == id) {
        info.level = 2;
        info.parentId = element.id;
        info.itemTotal = element.children.length;
        info.AuxiliaryTotal = el.auxiliaryList.length;
        if (el.children.length === 0 && el.auxiliaryList.length === 0) {
          info.isDelete = true;
        }
        if (AuxiliaryIndex !== -1) {
          info.isDelete = true;
          info.AuxiliaryId = el.auxiliaryList[AuxiliaryIndex].id;
        }
      }
      el.children.forEach((arr, arrIndex) => {
        if (arr.id == id) {
          info.level = 3;
          info.parentId = el.id;
          info.itemTotal = el.children.length;
          info.AuxiliaryTotal = arr.auxiliaryList.length;
          if (arr.auxiliaryList.length <= 0) {
            info.isDelete = true;
          }
          if (AuxiliaryIndex !== -1) {
            info.isDelete = true;
            info.AuxiliaryId = arr.auxiliaryList[AuxiliaryIndex].id;
          }
        }
      });
    });
  });

  return info;
};
export const getCabinetMinWidth = (type, id) => {
  //获取展柜最小宽度
  if (type === 'cabinet') {
    if (id == 17001) {
      return 2.575 
    }
    if (id == 47001 || id == 47002) {
      return 2.95572;
    }
    if (id == 37005 || id == 37006) {
      if (id == 57005) {
        return 3.8;
      }
      if (id == 57006) {
        return 3.8;
      }

      return 1.406;
    }
    if (id == 57001 || id == 57002 || id == 57003) {
      if (id == 57002) {
        return 2.0138;
      }
      if (id == 57003) {
        return 2.991;
      }
      if (id == 57004) {
        return 1.6236;
      }
      return 1.406;
    }
    if (id == 67001 || id == 67002 || id == 67003 || id == 67004) {
      if (id == 67001) {
        return 1.73056;
      }
      if (id == 67002) {
        return 2.34097;
      }
      if (id == 67003) {
        return 3.02054;
      }
      if (id == 67004) {
        return 3.4693;
      }
    }
    if (id == 77001 || id == 77002) {
      if (id == 77001) {
        return 1.98;
      }
      if (id == 77002) {
        return 2.38;
      }
    }
    if (id == 87003) {
      return 4.3;
    }
    if (id == 87004) {
      return 2.7;
    }
    if (id == 107001 || id == 107002 || id == 107003) {
      if (id == 107001) {
        return 2.1;
      }
      if (id == 107002) {
        return 2.81;
      }
      if (id == 107003) {
        return 2.375;
      }
    }
    if (id == 127001 || id == 127002) {
      if (id == 127001) {
        return 1.7;
      }
      if (id == 127002) {
        return 1.8583;
      }
    }
    if (id == 157001 || id == 157002) {
      if (id == 157001) {
        return 4.1666;
      }
      if (id == 157002) {
        return 2.9;
      }
    }
    if (id == 137001 || id == 137002 || id == 137003 || id == 137004) {
      if (id == 137001) {
        return 2.79027;
      }
      if (id == 137002) {
        return 3.29848;
      }
      if (id == 137003) {
        return 3.25938;
      }
      if (id == 137004) {
        return 3.16165;
      }
    }
    if (id == 167001 || id == 167002) {
      return 2.9955;
    }
    return 1.31;
  }
  return 0;
};
export const getCabinetMinHeight = (type, id) => {
  //获取展柜最小高度
  if (type === 'cabinet') {
    if (id === 37001 || id === 37002) {
      return 1.66;
    } else if (id === 37003 || id === 37004) {
      return 2.06;
    }
    if (id == 37005 || id == 37006) {
      if (id == 57005) {
        return 2.8;
      }
      if (id == 57006) {
        return 2.8;
      }

      return 1.406;
    }
    if (id == 87003) {
      return 4;
    }
    if (id == 87004) {
      return 3.9;
    }
    if (id == 57001 || id == 57002 || id == 57003) {
      if (id == 57002) {
        return 3.2;
      }
      if (id == 57003 || id == 57004) {
        return 3.1166;
      }
      return 3.504;
    }
    if (id == 77001 || id == 77002) {
      if (id == 77001) {
        return 2.1763;
      }
      if (id == 77002) {
        return 2.1;
      }
    }
    if (id == 107001 || id == 107002 || id == 107003) {
      if (id == 107001) {
        return 3.723;
      }
      if (id == 107002) {
        return 2.28;
      }
      if (id == 107003) {
        return 2.76;
      }
    }
    if (id == 127001 || id == 127002) {
      if (id == 127001) {
        return 3.8791;
      }
      if (id == 127002) {
        return 4.2958;
      }
    }
    if (id == 157001 || id == 157002) {
      if (id == 157001) {
        return 3.7222;
      }
      if (id == 157002) {
        return 2.63;
      }
    }
    if (id == 167001 || id == 167002) {
      return 2.59253;
    }
  }
  return 0;
};
